package tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : ServerTest
 * @Author : 54061
 * @Date: 2022/2/25 19:57
 */
public class ServerTest {
    public static void main(String[] args) throws IOException {
        System.out.println("开启服务端");
        //开启服务端
        ServerSocket serverSocket = new ServerSocket(6666);
        //获取客户端发送的连接请求,若未接收到任何客户端发送的请求.则将处于阻塞状态
        Socket server = serverSocket.accept();
        //获取服务端套接字的输出流
        InputStream is = server.getInputStream();
        //读取客户端发送的数据
        byte[] b = new byte[1024];
        int length = is.read(b);
        System.out.println("new = " + new String(b,0,length));
        //获取输出流
        OutputStream os = server.getOutputStream();
        os.write("hello too,TCP client".getBytes());
        //关闭资源
        os.close();
        is.close();
        server.close();
        serverSocket.close();
        System.out.println("关闭服务端");
    }
}
